package org.jeecg.modules.icon.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.core.constant.DataStatusEnum;
import org.jeecg.modules.icon.entity.BimGeoIcon;
import org.jeecg.modules.icon.mapper.BimGeoIconMapper;
import org.jeecg.modules.icon.service.IBimGeoIconService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @Description: bim_geo_icon
 * @Author: jeecg-boot
 * @Date: 2022-12-06
 * @Version: V1.0
 */
@Service
@Slf4j
public class BimGeoIconServiceImpl extends ServiceImpl<BimGeoIconMapper, BimGeoIcon> implements IBimGeoIconService {

    @Override
    public void add(BimGeoIcon bimGeoIcon) {
        BimGeoIcon oneVerify = this.findOneVerify(bimGeoIcon);
        if (oneVerify != null) {
            log.error("添加Icon配置失败，Icon配置已存在！param=>{}", JSON.toJSONString(bimGeoIcon));
            throw new JeecgBootException("添加Icon配置失败，Icon配置已存在！");
        }
        //删除状态
        bimGeoIcon.setDelFlag(DataStatusEnum.NORMAL.getCode());
        this.save(bimGeoIcon);
    }

    @Override
    public void update(BimGeoIcon bimGeoIcon) {
        if (bimGeoIcon.getId() == null) {
            log.error("修改Icon配置失败，Id不能为空！！param=>{}", JSON.toJSONString(bimGeoIcon));
            throw new JeecgBootException("修改Icon配置失败，Id不能为空！");
        }
        BimGeoIcon oneVerify = this.findOneVerify(bimGeoIcon);
        if (oneVerify != null && (!oneVerify.getId().equals(bimGeoIcon.getId()))) {
            log.error("修改Icon配置失败，Icon配置已存在！param=>{}", JSON.toJSONString(bimGeoIcon));
            throw new JeecgBootException("修改Icon配置失败，Icon配置已存在！");
        }
        this.updateById(bimGeoIcon);
    }

    private BimGeoIcon findOneVerify(BimGeoIcon bimGeoIcon) {
        LambdaUpdateWrapper<BimGeoIcon> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(bimGeoIcon.getUniqueCode()), BimGeoIcon::getUniqueCode, bimGeoIcon.getUniqueCode());
        List<BimGeoIcon> bimGeoIcons = this.getBaseMapper().selectList(queryWrapper);
        return CollectionUtils.isEmpty(bimGeoIcons) ? null : bimGeoIcons.get(0);
    }

}
